# pip install PyExecJS
# pip install jsbeautifier

# import execjs

# # 假设这里是从网页中获取的 JavaScript 代码片段
# javascript_code = """
# try {
#     window = {};
#     window.__NUXT__ = {"a":"str"}
#     console.log(JSON.stringify(window.__NUXT__));
# } catch (error) {
#     console.error('Error executing JavaScript:', error);
# }
# """

# # 使用 Node.js 作为执行上下文环境
# # context = execjs.get('Node').compile(javascript_code)
# context = execjs.compile(javascript_code)

# try:
#     # 执行 JavaScript 代码并获取 window.__NUXT__ 的值
#     nuxt_data = context.eval('window.__NUXT__')
#     print(nuxt_data)
# except execjs.RuntimeError as e:
#     print(f"Error evaluating JavaScript: {e}")



# pip install jsbeautifier
# pip install js2py


import jsbeautifier

# 这里是混淆压缩后的JavaScript代码
compressed_js = r"""
window.__NUXT__=(function(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G){u.source_type=b;u.search_type="filter";u.v="2.0";u.hp=k;u.hp_page="Anjuke_Hp_View";u.esf_id=b;u.isauction=b;u.pos=b;u.trading_area_ids=b;u.found=b;u.prop_expire=b;u.entry=102;u.proids=a;C.province="辽宁";C.city=r;C.coord="123.43739,41.812086";return {layout:"default",data:[{props:{total:m,page:k,has_more:m,list:[],soj_info:u},names:n,mainValue:{post_url:"https:\u002F\u002Fpost.58.com\u002Ffang\u002F2\u002F12\u002Fs5?PGTID=0d40000c-0188-91db-e556-17c15a7657aa&ClickID=1",qr_code:{twUrl:d,extParams:{qr_type:"pc_esfjjr",broker_id:o,Web_Broker_ShopPage_dianhua:o,soj_flag:"{}",ft:"ft",city_id:"188",call_up:k},redirectTo:"\u002Fpage\u002FtaroPage\u002Fbrokers\u002Fpages\u002Fview\u002Fview"},broker:{main_info:{broker_id:203566346,name:"陈卫",company_id:497618,photo:"https:\u002F\u002Fspic1.ajkimg.com\u002FpUvDRWPA9QF8fqjH6Wm5ldZH1IivFm17CWjABFdQW8CKGlPaUO99GdiyJp67iVLonjyvNCi0751i0eXrKdz3Wypr584SdaEDSLlJ44eSFMbVCwmyW_3R00rVh5Cfm4gmUjBYQCeYZxn2ilePXUm9KyIGxf2prGE6fxS4L9xpsXa1JQzU1B9cEJLxcJx5HF44",phone:d,valid_status:c,service_user_num:13,take_look_num:b,commission:b,seniority:"1-3年",is_trade_protection:c,is_show_guarantee:c,show_guarantee_action:d,billboard_logo:d,familiar_community:[v,w,x],familiar_community_info:[{id:2786714,name:v,url:d},{id:2859908,name:w,url:d},{id:2273204,name:x,url:d}],familiar_shangquan:["浑南-东湖","浑南-棋盘山","沈阳周边-抚顺"],company_full_name:l,company_url:d,store_name:"芳宁房产总店",city_id:p,store_id:1031624,goddess:d,shop_status:e,touch_url:"https:\u002F\u002Fm.58.com\u002Fsy\u002Fbroker-detail\u002F203566346.shtml",company_name:"芳宁房产",labels:[y,z,A],web_url:f,weapp_url:d,goddess_medal:[{andromedaScore:"441",medalType:b,cityScoreRank:"56.0%",serviceAbilityLevel:i,houseinfoRealLevel:i,professionQualityLevel:i,cate:b},{andromedaScore:"115",medalType:b,cityScoreRank:"10.0%",serviceAbilityLevel:i,houseinfoRealLevel:i,professionQualityLevel:"良好",cate:e}],view_type:e,weiliao_action:a,app_detail_action:a,reply_time:b},other_info:{service_tag:[y,z,A],unified_broker_verification:[],company_license:{href:"https:\u002F\u002Ffangfe.58.com\u002Fspage\u002Flicense\u002Fcard?buzType=esf&city_id=188&broker_id=0&company_id=497618&site=wuba&store_id=0&type=1",code:"91210112MAC9YWGNX9",image_url:a},unified_company_verification:[],medal_info:{is_community_expert:d,is_talent:g,is_senior:g,is_active_prospector:c,is_shopkeeper_rec:c,is_card_auth:c,is_dating_talent:c},evaluation:{good_ratio:"100%",total_num:9,latest_comment:[{user_name:j,score:h,content:B},{user_name:j,score:h,content:q},{user_name:j,score:h,content:q},{user_name:j,score:h,content:q},{user_name:j,score:h,content:B}]},chat_href:"wbmain:\u002F\u002Fjump\u002Fsecondhouse\u002Fbroker_info?params=%7B%22commondata%22%3A%7B%7D%2C%22soj_info%22%3A%7B%7D%2C%22full_path%22%3A%22%22%2C%22jump_log%22%3A%22%22%2C%22broker_id%22%3A%22203566346%22%2C%22title%22%3A%22%22%7D&isFinish=false&needLogin=false&isBackToMain=false&isSlideinBottom=false&isNoAnimated=false"},house_info:{esf_count:78,zf_count:b,xf_count:b,shop_count:b,office_count:b,workshop_count:b},seo_info:C},main_url:f,app_download_url:"https:\u002F\u002Fdown.58.com\u002Fhome.html?from=pc_topbar_icon_58app"},recommend:[{phone:a,broker_id:202419296,name:"杨立军",photo:"https:\u002F\u002Fspic1.ajkimg.com\u002FuBddSw2XeRgJcTFIzerKv6wKZCfV57ShcHnF_2d8kxHVBboXTzi6mLJ9hVApGtadH8GW6FFdhYMIv1tb-_mPEpQiUKgUOOBYKN8ZBUWg_zkQzGlR5jgZdxzcfHurNMiviKreWJJ5xDy36lgkKhfeRKA80xKTyKQcyWdByzKpF27DkNwFeZUWElJ9MHp_P3JY",score:D,company_full_name:l,broker_url:"https:\u002F\u002Fbroker.58.com\u002Fsy\u002Fdetail\u002F202419296.shtml",commission:b,supplement_reason:s,chat_href:"wbmain:\u002F\u002Fjump\u002Fsecondhouse\u002Fbroker_info?params=%7B%22commondata%22%3A%7B%7D%2C%22soj_info%22%3A%7B%7D%2C%22full_path%22%3A%22%22%2C%22jump_log%22%3A%22%22%2C%22broker_id%22%3A%22202419296%22%2C%22title%22%3A%22%E6%9D%A8%E7%AB%8B%E5%86%9B%22%7D&isFinish=false&needLogin=false&isBackToMain=false&isSlideinBottom=false&isNoAnimated=false",is_trade_protection:c},{phone:a,broker_id:203566626,name:"邓小明",photo:"https:\u002F\u002Fspic1.ajkimg.com\u002F3EtDaU0Fmy2XLv2AoWmeTTeOlYs7jgglQGrnLJxRnuFRvvwWM-SldNHt_iIxtYjwBjBSwYc4YQJdAntQq6ClOyLvwXIW8Md4qkmulF6eNFM4LIiTGv_cAA5--GBnl-919bD1KOgoGlQ2OEesa3M5e52_3Iok1qTk71JNHwYdrcyjRDzzZV8-ofSmZwT7wNbE",score:D,company_full_name:l,broker_url:"https:\u002F\u002Fbroker.58.com\u002Fsy\u002Fdetail\u002F203566626.shtml",commission:b,supplement_reason:s,chat_href:"wbmain:\u002F\u002Fjump\u002Fsecondhouse\u002Fbroker_info?params=%7B%22commondata%22%3A%7B%7D%2C%22soj_info%22%3A%7B%7D%2C%22full_path%22%3A%22%22%2C%22jump_log%22%3A%22%22%2C%22broker_id%22%3A%22203566626%22%2C%22title%22%3A%22%E9%82%93%E5%B0%8F%E6%98%8E%22%7D&isFinish=false&needLogin=false&isBackToMain=false&isSlideinBottom=false&isNoAnimated=false",is_trade_protection:c},{phone:a,broker_id:200249604,name:"关云",photo:"https:\u002F\u002Fspic1.ajkimg.com\u002F2ICXBfPbSBcZDxQ2xkBb3XU_6jzlJSM7mQWW0dCMMYCci6h1wYzoeOav75wTGLUSSBC9Ulpe5QGUmbzaX-V_8Ge75IF-8Vs5zZnVfxuLkIG4iqmcjuQUzGqcbrJvvvnBMtDTRaDkxMm9GxwOPjn5gEXsHroQHI2TjCJ3anu8pKmEVsFWKVEfvQsyv4e-9OeL",score:h,company_full_name:l,broker_url:"https:\u002F\u002Fbroker.58.com\u002Fsy\u002Fdetail\u002F200249604.shtml",commission:b,supplement_reason:s,chat_href:"wbmain:\u002F\u002Fjump\u002Fsecondhouse\u002Fbroker_info?params=%7B%22commondata%22%3A%7B%7D%2C%22soj_info%22%3A%7B%7D%2C%22full_path%22%3A%22%22%2C%22jump_log%22%3A%22%22%2C%22broker_id%22%3A%22200249604%22%2C%22title%22%3A%22%E5%85%B3%E4%BA%91%22%7D&isFinish=false&needLogin=false&isBackToMain=false&isSlideinBottom=false&isNoAnimated=false",is_trade_protection:c}],navInfo:d,cityGroup:d,breadCrumb:{bread_crumb:[{title:"沈阳房产网",url:"https:\u002F\u002Fsy.58.com\u002Fershoufang\u002F"},{title:"沈阳经纪人",url:"https:\u002F\u002Fbroker.58.com\u002Fsy\u002Flist\u002F"},{title:"陈卫的店铺",url:f}]},seo:d,resolveParams:{url:f,cate:a,o:a,p:e},config:{cityId:p,brokerId:o,curPage:e,cate:a,type:n,sort:a,path:f},sojParams:{plat:k,site:t,p:E,pn:E,cp:u},trackUrl:"{\"site\":\"58\",\"cate\":\"1,12\",\"pagetype\":\"\",\"page\":\"\",\"pagesousuo\":\"pc_esf_jingjiren_danye\"}",isBrokerView:g,valid:c,href:f,seoInfo:C,xfProperty:d,isWuba:g}],fetch:[],error:d,state:{galleryMainImage:a,galleryList:{array:[]},galleryBigList:{array:[]},galleryLayout:[],galleryHouse:[],galleryEnvironment:[],galleryNowEnum:"-1",galleryRealPreCountNow:e,feCommon:{guid:"BC2B35CF-F14B-48CE-A713-EBE4F3287757",yun:{hostname:"hbg-web-hbghousepc-3-109078-sgvrk",wcloud_cluster_group:t,wcloud_imagefourversion:"4.6.42.0",wcloud_wmonitor_cluster:"hbg_web_hbghousepc"},location:{lat:a,lon:a},app:{os:F,version:a,type:a,value:a,originType:a,originValue:a,originVersion:a,appCommonParams:{},imei:a,platform:a,APP_TYPES:{ANJUKE:"anjuke",WUBA:t,WBUTOWN:"wbutown",BROKER:"broker",AJLZ:"ajlz",WEIXIN:"weixin",MINIPROGRAM:"miniprogram"},isAnjuke:c,isBroker:c,isWuba:c,isWeixin:c,isMiniprogram:c,isAJLZ:c,isWeb:g},source_h5:{app:a,ver:a,os:F,udid2:a,macid:a,guid:a},auth:{node_member_id:m,PPU:n},cityGateway:{anjuke:{id:39,name:r,alias:"shen",pinyin:G},wuba:{id:p,name:r,alias:G,pinyin:a}},clientIP:"175.164.153.87, 10.135.7.42",origin:"https:\u002F\u002Fbroker.58.com",ua:"Mozilla\u002F5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\u002F537.36 (KHTML, like Gecko) Chrome\u002F126.0.0.0 Safari\u002F537.36 Edg\u002F126.0.0.0"}},serverRendered:g,routePath:"\u002Fsy\u002Fdetail\u002F203566346.shtml",config:{}}}("",0,false,null,1,"https:\u002F\u002Fbroker.58.com\u002Fsy\u002Fdetail\u002F203566346.shtml",true,5,"一般","匿名用户","1","沈阳芳宁房产中介有限公司","0",void 0,"203566346",188,"非常好；服务态度满意、业务水平满意、回复速度及时","沈阳","同门店经纪人","wuba",{},"荣盛锦绣观邸(二期)","汇置城","荣盛锦绣观邸(一期)","专车接送","新房买卖","全城看房","非常好；问题解答满意、服务态度满意、业务水平满意",{},4.8,"Ershou_Web_Broker_ShopPage","win","sy"));
"""

# processed_string = repr(compressed_js)

# # 使用jsbeautifier美化JavaScript代码
# opts = jsbeautifier.default_options()
# opts.indent_size = 2  # 设置缩进大小
# beautified_js = jsbeautifier.beautify(compressed_js, opts)
# # 输出美化后的JavaScript代码
# # print("美化后的JavaScript代码:")
# # print(beautified_js)


# # 将JavaScript代码写入文件
# with open('compressed.js', 'w', encoding='utf-8') as file:
#     file.write(compressed_js)

# 使用jsbeautifier美化JavaScript代码
opts = jsbeautifier.default_options()
opts.indent_size = 2  # 设置缩进大小
beautified_js = jsbeautifier.beautify(compressed_js, opts)
# 输出美化后的JavaScript代码
# print("美化后的JavaScript代码:")
# print(beautified_js)

# # 将美化后的JavaScript代码写入新的文件
# with open('beautified.js', 'w', encoding='utf-8') as file:
#     file.write(beautified_js)

# print("JavaScript代码已美化并保存为beautified.js")




# # import jsbeautifier

# # 读取混淆压缩后的JavaScript代码
# with open('compressed.js', 'r', encoding='utf-8') as file:
#     compressed_js = file.read()

# # 使用jsbeautifier美化JavaScript代码
# opts = jsbeautifier.default_options()
# opts.indent_size = 2  # 设置缩进大小
# beautified_js = jsbeautifier.beautify(compressed_js, opts)

# # 将美化后的JavaScript代码写入新的文件
# with open('beautified.js', 'w', encoding='utf-8') as file:
#     file.write(beautified_js)

# print("JavaScript代码已美化并保存为beautified.js")



import js2py

# # 读取混淆压缩后的JavaScript代码
# with open('beautified.js', 'r', encoding='utf-8') as file:
#     compressed_js = file.read()


js_code = f"""
window = {{}}; 
{beautified_js}
"""

context = js2py.EvalJs()

# 执行JavaScript代码
context.execute(js_code)

# 调用JavaScript函数并获取结果
# result = context.res_func()
result = context.eval('window.__NUXT__.data[0].seoInfo')

# 输出结果
print("window.__NUXT__的结果是:", result)
